package com.yun.zf.admin.quartz.task;

import com.yun.zf.admin.service.ZfCommunityStatisticalService;
import com.yun.zf.admin.service.ZfHouseBasicService;
import com.yun.zf.common.app.domain.ZfCity;
import com.yun.zf.common.app.domain.ZfCommunityStatistical;
import com.yun.zf.common.app.dto.community.CommunityTotal;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.system.service.ZfCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private ZfHouseBasicService houseBasicService;
    @Autowired
    private ZfCityService cityService;
    @Autowired
    private ZfCommunityStatisticalService statisticalService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }


    /**
     * 统计小区房源信息
     */
    @Transactional
    public  void  statistical(){

        // 删除之前数据
        statisticalService.deleteFromDate(new Date());
        //1、获取已开通城市信息
        List<ZfCity> cityList = cityService.getIsOpenCity();
        //1 、 获取小区统计信息
        for (ZfCity city: cityList
             ) {
          List<CommunityTotal> brandHouse =  houseBasicService.brandHouseTotal(city.getPid(),city.getCityCode());
            // 取出所有的统计信息，将communityId 为同一个的放入同一条数据中
            Map<String, ZfCommunityStatistical> map = new HashMap<>();
            String key ="";
            for (CommunityTotal total: brandHouse
                 ) {
                Long communityid =total.getCommunityId();
                StringBuffer b = new StringBuffer(String.valueOf(communityid));
                b.append("_").append(total.getType());
                key =b.toString();
                ZfCommunityStatistical statistical =null;
                if (map.containsKey(key)){
                     statistical = map.get(communityid);
                }else {
                   statistical =  new ZfCommunityStatistical();
                    statistical.setCommunityId(communityid);
                }
                statistical.setType(total.getType());
                statistical.setTotal(total.getTotal());
                // 统计均价及最低最高
                CommunityTotal maxAndMin =    houseBasicService.getDetailMaxAndMin(city.getPid(),city.getCityCode(),communityid,total.getType());
                statistical.setAveragePrice(maxAndMin.getAveragePrice());
                statistical.setMinArea(maxAndMin.getMinArea());
                statistical.setMinPrice(maxAndMin.getMinPrice());
                statistical.setMaxArea(maxAndMin.getMaxArea());
                statistical.setMaxPrice(maxAndMin.getMaxPrice());
                if (total.getType()==1){
                    statistical.setPriceUnit("元");
                }else if (total.getType()==2){
                    statistical.setPriceUnit("万元");
                }
                map.put(key,statistical);

            }
            List<ZfCommunityStatistical> statisticalList = new ArrayList<>();
            for(String temp:map.keySet()){
                statisticalList.add(map.get(temp));
            }
            statisticalService.saveBatch(statisticalList);

        }


    }




}
